package com.yk.controller;

import com.yk.pojo.Result;
import com.yk.pojo.User;
import com.yk.service.UserService;

import jakarta.validation.constraints.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
@Validated
public class UserController {
    @Autowired
   private UserService userService;

    //注册
    @PostMapping("/register")
    public Result register(@Pattern(regexp = "^\\S{5,16}$")String username, @Pattern(regexp = "^\\S{5,16}$") String password){

        //if(username!=null&&username.length()>=5&&username.length()<=16&&password!=null&&password.length()>=5&&password.length()<=16){
        //查询用户
        User u =  userService.findByUserName(username);
        if(u==null){
            //没有占用
            //注册
            userService.register(username,password);
            return Result.success();
        }else{
            //占用
            return Result.error("用户名已被占用!");
        }
        // }else{
        //     return Result.error("参数不合法(密码和用户名长度大于等于5小于16)");
        // }


    }

}
